#include<iostream>
using namespace std;
int n,father[30001],dist[30001],size[30001],i,y,a,b;
string s;
void init();
int get(int x);
void merge();
void check();
int main(){
	init();
	cin>>n;
	while(n--){
		cin>>s>>a>>b;
		if(s=="M")
			merge();
		else
			check();
	}
	return 0;
}
void init(){
	for(i=1;i<30001;++i){
		father[i]=i;
		dist[i]=0;
		size[i]=1;
	}
}
int get(int x){
	if(x==father[x])return x;
	y=father[x];
	father[x]=get(y);
	dist[x]+=dist[y];
	return father[x];
}
void merge(){
	a=get(a);
	b=get(b);
	father[a]=b;
	dist[a]=size[b];
	size[b]+=size[a];
}
void check(){
	if(get(a)!=get(b))
		cout<<-1<<endl;
	else{
		cout<<(dist[a]>dist[b]?(dist[a]-dist[b]):(dist[b]-dist[a]))<<endl;
	}
}